import express, { Router } from 'express';
import { ResponseHelper } from '../utils/response';
import { generateToken } from '../middleware/auth';

const router: Router = express.Router();

// 硬编码的用户凭据
const ADMIN_CREDENTIALS = {
  username: 'root',
  password: 'root123@'
};

/**
 * @swagger
 * components:
 *   schemas:
 *     LoginRequest:
 *       type: object
 *       required:
 *         - username
 *         - password
 *       properties:
 *         username:
 *           type: string
 *           description: 用户名
 *           example: "root"
 *         password:
 *           type: string
 *           description: 密码
 *           example: "root123@"
 *     LoginResponse:
 *       type: object
 *       properties:
 *         success:
 *           type: boolean
 *           example: true
 *         message:
 *           type: string
 *           example: "登录成功"
 *         data:
 *           type: object
 *           properties:
 *             token:
 *               type: string
 *               description: JWT令牌
 *               example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
 *             user:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "admin"
 *                 username:
 *                   type: string
 *                   example: "root"
 *         timestamp:
 *           type: string
 *           format: date-time
 */

/**
 * @swagger
 * /api/auth/login:
 *   post:
 *     summary: 用户登录
 *     description: 使用用户名和密码进行登录，返回JWT令牌
 *     tags: [Authentication]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/LoginRequest'
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/LoginResponse'
 *       400:
 *         description: 请求参数错误
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "用户名和密码不能为空"
 *       401:
 *         description: 登录失败
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "用户名或密码错误"
 */
router.post('/login', async (req, res) => {
  try {
    const { username, password } = req.body;

    // 验证输入参数
    if (!username || !password) {
      return ResponseHelper.error(res, '用户名和密码不能为空', 400);
    }

    // 验证用户名和密码
    if (username !== ADMIN_CREDENTIALS.username || password !== ADMIN_CREDENTIALS.password) {
      return ResponseHelper.error(res, '用户名或密码错误', 401);
    }

    // 生成JWT令牌，有效期7天
    const token = generateToken({
      user_id: 'admin',
      username: username
    }, '7d');

    // 返回登录成功响应
    return ResponseHelper.success(res, {
      token,
      user: {
        id: 'admin',
        username: username
      }
    }, '登录成功');

  } catch (error) {
    console.error('登录失败:', error);
    return ResponseHelper.error(res, '服务器内部错误', 500);
  }
});

export default router;